<?php
#[分类管理]
$sysnav = "cate";

require_once("class/unlimited_category.class.php");
$CT = new Category();
$sysgroupid = intval($sysgroupid);
if($sysgroupid)
{
	#[判断权限]
	$m = "msg_".$sysgroupid;
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP[$m])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
}

function LoopUpdateCategory($rootid,$parentid)
{
	global $language;
	global $DB,$prefix;
	if(is_array($parentid))
	{
		$pid = implode(",",$parentid);
	}
	else
	{
		$pid = intval($parentid);
	}
	if($pid)
	{
		$rs = $DB->qgGetAll("SELECT id FROM ".$prefix."category WHERE parentid IN(".$pid.") AND language='".$language."'");
		if($rs)
		{
			$array = array();
			foreach($rs AS $key=>$value)
			{
				$array[] = $value["id"];
			}
			$updateId = implode(",",$array);
			$DB->qgQuery("UPDATE ".$prefix."category SET rootid='".$rootid."' WHERE parentid IN(".$updateId.")");
			LoopUpdateCategory($rootid,$array);
		}
	}
	return true;
}


#[添加分类]
if($sysAct == "add")
{
	if(!$system && file_exists("data/system_1.php"))
	{
		include_once("data/system_1.php");
	}
	#[如果不存在sysgroupid且不存在parentid]
	if(!$sysgroupid && !$cateid)
	{
		Error("操作不正确！",$mainlink."?file=sysgroup&act=list");
	}
	if($cateid)
	{
		#[获取父分类的信息]
		$rs = $DB->qgGetOne("SELECT id,sysgroupid,rootid,parentid,catename,catename_en,catename_ft FROM ".$prefix."category WHERE id='".$cateid."' AND language='".$language."'");
		$catename = $rs["catename"];
		$catename_en = $rs["catename_en"];
		$catename_ft = $rs["catename_ft"];
		$sysgroupid = $rs["sysgroupid"];
		$rootid = $rs["rootid"];
		$parentid = $rs["parentid"];
		unset($rs);
		#[判断权限]
		if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
		{
			Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
		}

	}
	#[如果sysgroupid]
	if($sysgroupid)
	{
		#[判断权限]
		if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
		{
			Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
		}

		$rs = $DB->qgGetOne("SELECT groupname,sign FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'");
		$groupname = $rs["groupname"];
		$groupsign = $rs["sign"];
		unset($rs);
		if(!$cateid)
		{
			#[获取当前组的分类管理]
			$rslist = $DB->qgGetAll("SELECT id,rootid,parentid,catename,catename_en,catename_ft FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."'");
			$catelist = $CT->arraySet($rslist,0);
			unset($rslist);
		}
	}
	
	#[字段方案]
	$field_count = $DB->qg_count("SELECT count(*) FROM ".$prefix."sysgroup_field WHERE sysgroupid='".$sysgroupid."' AND ifcheck=1");
	if($field_count>0)
	{
		$fieldgroup = $DB->qgGetAll("SELECT * FROM ".$prefix."sysgroup_fieldgroup WHERE sysgroupid='".$sysgroupid."' ORDER BY id ASC");
	}
	
	Foot("category.view.qg");
}
elseif($sysAct == "modify")
{
	#[编辑分类]
	$id = intval($id);
	if(!$id)
	{
		Error("无法获取分类ID信息",$mainlink."?file=sysgroup&act=list");
	}
	#[获取分类信息]
	$rs = $DB->qgGetOne("SELECT * FROM ".$prefix."category WHERE id='".$id."' AND language='".$language."'");
	if(!$rs)
	{
		Error("无法获取数据...",$mainlink."?file=sysgroup&act=list");
	}
	$sysgroupid = $rs["sysgroupid"];
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	$rsGroup = $DB->qgGetOne("SELECT groupname,sign FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'");
	$groupname = $rsGroup["groupname"];
	$groupsign = $rsGroup["sign"];
	unset($rsGroup);

	$rslist = $DB->qgGetAll("SELECT id,rootid,parentid,catename,catename_en,catename_ft FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND id!='".$id."' AND language='".$language."'");
	$catelist = $CT->arraySet($rslist,0);
	unset($rslist);
	#[显示样式]
	$rsC = $DB->qgGetOne("SELECT * FROM ".$prefix."category WHERE id='".$id."'");
	#[缩略图]
	$rsd = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id='".$rs["thumb"]."'");
	$rsd2 = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id='".$rs["thumb2"]."'");
	$rsd3 = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id='".$rs["thumb3"]."'");
	$content = FckToHtml($rsC["note"]);
	$content_en = FckToHtml($rsC["note_en"]);
	$content_ft = FckToHtml($rsC["note_ft"]);
	
	#[字段方案]
	$field_count = $DB->qg_count("SELECT count(*) FROM ".$prefix."sysgroup_field WHERE sysgroupid='".$sysgroupid."' AND ifcheck=1");
	if($field_count>0)
	{
		$fieldgroup = $DB->qgGetAll("SELECT * FROM ".$prefix."sysgroup_fieldgroup WHERE sysgroupid='".$sysgroupid."' ORDER BY id ASC");
	}
	
	Foot("category.view.qg");
}
elseif($sysAct == "viewok")
{
	if(!$sysgroupid)
	{
		Error("操作不正确！",$mainlink."?file=sysgroup&act=list");
	}
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	$id = intval($id);
	if($id)
	{
		$react = "modify";
	}
	else
	{
		$react= "add";
	}
	$sysgroupid = intval($sysgroupid);
	#[读取系统组图片尺寸]
	if($sysgroupid)
	{
		$picsize = picsize($sysgroupid);
	}
	
	$msg = $STR->safe($_POST);
	if($msg["catename"])
	{
		$updata["catename"] = $STR->safe($msg["catename"]);
	}
	if($msg["catename_en"])
	{
		$updata["catename_en"] = $STR->safe($msg["catename_en"]);
	}
	if($msg["catename_ft"])
	{
		$updata["catename_ft"] = $STR->safe($msg["catename_ft"]);
	}
	$updata["fieldgroupid"] = intval($msg["fieldgroupid"]);
	$updata["thumb"] = intval($msg["thumb"]);
	$updata["thumb2"] = intval($msg["thumb2"]);
	$updata["thumb3"] = intval($msg["thumb3"]);
	$updata["taxis"] = intval($msg["taxis"]);
	$updata["tpl_index"] = $STR->html($msg["tpl_index"]);
	$updata["tpl_list"] = $STR->html($msg["tpl_list"]);
	$updata["tpl_msg"] = $STR->html($msg["tpl_msg"]);
	$updata["status"] = intval($msg["status"]);
	$updata["psize"] = intval($msg["psize"]);
	#[SEO]
	$updata["meta_title"] = $STR->safe($meta_title);
	$updata["meta_keywords"] = $STR->safe($meta_keywords);
	$updata["meta_description"] = $STR->safe($meta_description);
	$updata["meta_title_en"] = $STR->safe($meta_title_en);
	$updata["meta_keywords_en"] = $STR->safe($meta_keywords_en);
	$updata["meta_description_en"] = $STR->safe($meta_description_en);
	$updata["meta_title_ft"] = $STR->safe($meta_title_ft);
	$updata["meta_keywords_ft"] = $STR->safe($meta_keywords_ft);
	$updata["meta_description_ft"] = $STR->safe($meta_description_ft);
	#[内容]
	$updata["note"] = $STR->html($content);
	$updata["note_en"] = $STR->html($content_en);
	$updata["note_ft"] = $STR->html($content_ft);
	
	#[编辑]
	if($react == "modify")
	{
		$oldRs = $DB->qgGetOne("SELECT parentid FROM ".$prefix."category WHERE id='".$id."' AND language='".$language."'");
		$oldPid = $oldRs["parentid"];
		$newPid = $msg["cateid"];
		#[如果更改了新旧分类]
		if($newPid != $oldPid)
		{
			#[如果新的分类ID为根分类ID]
			if($newPid == 0 || !$newPid)
			{
				$parentid = 0;
				$rootid = 0;
			}
			else
			{
				#[获取新分类下的根分类ID号]
				$new_cate = $DB->qgGetOne("SELECT rootid FROM ".$prefix."category WHERE id='".$newPid."' AND language='".$language."'");
				#[修正移动分类时的错误]
				$rootid = $new_cate["rootid"] ? $new_cate["rootid"] : $newPid;
				#$rootid = $new_cate["rootid"] ? $new_cate["rootid"] : 0;
				$parentid = $newPid;
			}
			#[更新当前分类的父子关系]
			$DB->qgQuery("UPDATE ".$prefix."category SET rootid='".$rootid."',parentid='".$parentid."' WHERE id='".$id."'");
			#[如果新旧分类的rootid一样则进行以下操作]
			if($oldRs["rootid"] == $rootid)
			{
				$DB->qgQuery("UPDATE ".$prefix."category SET parentid='".$parentid."' WHERE parentid='".$id."'");
			}
			else
			{
				#[如果新旧分类不一致]
				if($rootid)
				{
					#[如果存在根分类ID]
					$DB->qgQuery("UPDATE ".$prefix."category SET rootid='".$rootid."' WHERE parentid='".$id."'");
					LoopUpdateCategory($rootid,$id);
				}
				else
				{
					$DB->qgQuery("UPDATE ".$prefix."category SET rootid='".$id."' WHERE parentid='".$id."'");
					LoopUpdateCategory($id,$id);
				}
			}
		}
		#[更新内容信息]
		$DB->update("category",$updata,array("id"=>$id));
		unset($updata);
		$reword = "分类信息编辑成功...";
	}
	#[添加]
	else
	{
		#[如果存在父分类]
		if($msg["cateid"])
		{
			$updata["parentid"] = $msg["cateid"];
			$rs = $DB->qgGetOne("SELECT rootid FROM ".$prefix."category WHERE id='".$msg["cateid"]."' AND language='".$language."'");
			if($rs["rootid"])
			{
				$updata["rootid"] = $rs["rootid"];
			}
			else
			{
				$updata["rootid"] = $msg["cateid"];
			}
		}
		else
		{
			$updata["rootid"] = 0;
			$updata["parentid"] = 0;
		}
		
		#[插入内容信息]
		$updata["sysgroupid"] = $sysgroupid;
		$updata["language"] = $language;
		$updata["status"] = 1;
		$DB->insert("category",$updata);
		unset($updata);
		$reword = "分类信息添加成功...";
	}
	
	#[把编辑器保存的图片存入数据库里]
	$imglist = editor_img($content.$content_en.$content_ft);
	if($imglist)
	{
		foreach($imglist AS $key=>$value)
		{
			$imgarray = explode("/",$value);
			$folder = $imgarray[0]."/".$imgarray[1]."/".$imgarray[2]."/";
			$sql = "SELECT * FROM ".$prefix."upfiles WHERE folder='".$folder."' AND filename='".$imgarray[3]."'";
			$check = $DB->qgGetOne($sql);
			if(!$check)
			{
				$filename = $imgarray[3];
				if(is_file($folder.$filename))
				{
					#[获取当前服务器信息]
					$mypath = $UP->getpath();
					#[生成缩略图]
					$thumbfile = $GD->thumb($mypath.$filename);
					#[生成水印图]
					$markfile = $GD->mark($mypath.$filename,$picsize["width"],$picsize["height"]);
					$filetype = $UP->qgfiletype($imgarray[3]);
					$sql = "INSERT INTO ".$prefix."upfiles(filetype,tmpname,filename,folder,postdate,thumbfile,markfile) VALUES('".$filetype."','".$filename."','".$filename."','".$folder."','".$system_time."','".$thumbfile."','".$markfile."')";
					$DB->qgQuery($sql);
					unset($imgarray,$sql,$check,$farray);
				}
			}
		}
	}
	
	Error($reword,$mainlink."?file=category&act=list&sysgroupid=".$sysgroupid);
}
elseif($sysAct == "orderlist")
{
	if(!$sysgroupid)
	{
		Error("操作不正确！",$mainlink."?file=sysgroup&act=list");
	}
	#[显示列表信息]
	$rslist = $DB->qgGetAll("SELECT * FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."' order by taxis asc");
	$msglist = $CT->arraySet($rslist,0);
	foreach($msglist AS $key=>$value)
	{
		if(!$value["catename"] && $value["catename_en"])
		{
			$value["catename"] = $value["catename_en"];
		}
		if(!$value["catename"] && $value["catename_ft"])
		{
			$value["catename"] = $value["catename_ft"];
		}
		$msglist[$key] = $value;
	}
	
	$query = "SELECT * FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' and parentid=0";
	$result=mysql_query($query);
	$number=mysql_num_rows($result);
	
	if($number==1)
	{
		$rs = $DB->qgGetOne("SELECT id FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' and parentid='0'");
		$parentid = $rs["id"];
	}
	else
	{
		$parentid = 0;
	}
	
	if($cateid)
	{
		$parentid = $cateid;
	}
		
	$ordermsglist = $DB->qgGetAll("SELECT * FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."' and parentid='".$parentid."' order by taxis asc");
	
	Foot("categoryorder.list.qg");
}
elseif($sysAct == "orderbyok")
{
	$i=0;
	$listlayer = substr($listlayer,0,(strlen($listlayer)-1));
	
	foreach(explode("=",$listlayer) as $updownlist)
	{
		$i=$i+1;
		$sql="UPDATE ".$prefix."category SET taxis='".$i."' WHERE id='".$updownlist."'";
		$DB->qgQuery($sql);
	}
	if($cateid)
	{
		Error("分类排序成功...",$mainlink."?file=category&act=orderlist&sysgroupid=".$sysgroupid."&cateid=".$cateid);
	}
	else
	{
		Error("分类排序成功...",$mainlink."?file=category&act=orderlist&sysgroupid=".$sysgroupid);
	}
}
elseif($sysAct == "addli")
{
	if(!$system && file_exists("data/system_1.php"))
	{
		include_once("data/system_1.php");
	}
	#[如果不存在sysgroupid且不存在parentid]
	if(!$sysgroupid && !$cateid)
	{
		Error("操作不正确！",$mainlink."?file=sysgroup&act=list");
	}
	if($cateid)
	{
		#[获取父分类的信息]
		$rs = $DB->qgGetOne("SELECT id,sysgroupid,rootid,parentid,catename,catename_en,catename_ft FROM ".$prefix."category WHERE id='".$cateid."' AND language='".$language."'");
		$catename = $rs["catename"];
		$catename_en = $rs["catename_en"];
		$catename_ft = $rs["catename_ft"];
		$sysgroupid = $rs["sysgroupid"];
		$rootid = $rs["rootid"];
		$parentid = $rs["parentid"];
		unset($rs);
		#[判断权限]
		if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
		{
			Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
		}

	}
	#[如果sysgroupid]
	if($sysgroupid)
	{
		#[判断权限]
		if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
		{
			Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
		}

		$rs = $DB->qgGetOne("SELECT groupname,sign FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'");
		$groupname = $rs["groupname"];
		$groupsign = $rs["sign"];
		unset($rs);
		if(!$cateid)
		{
			#[获取当前组的分类管理]
			$rslist = $DB->qgGetAll("SELECT id,rootid,parentid,catename,catename_en,catename_ft FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."'");
			$catelist = $CT->arraySet($rslist,0);
			unset($rslist);
		}
	}
	
	#[加载添加分类的模板]
	Foot("category.addli.qg");
}
elseif($sysAct == "addliok")
{
	$msg = $STR->safe($_POST);
	#[如果存在父分类]
	if($msg["cateid"])
	{
		$msg["parentid"] = $msg["cateid"];
		$rs = $DB->qgGetOne("SELECT rootid FROM ".$prefix."category WHERE id='".$msg["cateid"]."' AND language='".$language."'");
		
		#[获取已有分类个数]
		$sql0 = "SELECT count(id) FROM ".$prefix."category WHERE parentid='".$msg["cateid"]."' AND language='".$language."'";
		$cat_count = $DB->qg_count($sql0);
		
		if($rs["rootid"])
		{
			$msg["rootid"] = $rs["rootid"];
		}
		else
		{
			$msg["rootid"] = $msg["cateid"];
		}
	}
	else
	{
		$msg["rootid"] = 0;
		$msg["parentid"] = 0;
		
		#[获取已有分类个数]
		$sql0 = "SELECT count(id) FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."'";
		$cat_count = $DB->qg_count($sql0);
	}
	
	if($msg["catename"]){$catename = explode("\n",$msg["catename"]);}
	if($msg["catename_en"]){$catename_en = explode("\n",$msg["catename_en"]);}
	if($msg["catename_ft"]){$catename_ft = explode("\n",$msg["catename_ft"]);}
	
	#[存在简体版的情况]
	if($catename)
	{
		foreach($catename AS $key=>$value)
		{
			$allcate[$key]["cn"] = $value;
			if($catename_en)
			{
				$allcate[$key]["en"] = $catename_en[$key];
			}
			if($catename_ft)
			{
				$allcate[$key]["ft"] = $catename_ft[$key];
			}
		}
	}
	
	#[不存在简体版的情况]
	else
	{
		#[存在英文]
		if($catename_en)
		{
			$catename = $catename_en;
			foreach($catename AS $key=>$value)
			{
				$allcate[$key]["en"] = $value;
				if($catename_ft)
				{
					$allcate[$key]["ft"] = $catename_ft[$key];
				}
			}
		}
		#[只有繁体]
		else
		{
			$catename = $catename_ft;
			foreach($catename AS $key=>$value)
			{
				$allcate[$key]["ft"] = $value;
			}
		}
	}
	
	foreach($allcate AS $key=>$value)
	{
		$cat_count++;
		$sql = "INSERT INTO ".$prefix."category(sysgroupid,rootid,parentid,catename,catename_en,catename_ft,status,language,taxis) VALUES('".$msg["sysgroupid"]."','".$msg["rootid"]."','".$msg["parentid"]."','".$value["cn"]."','".$value["en"]."','".$value["ft"]."','1','".$language."','".$cat_count."')";
		$DB->qgQuery($sql);
	}
	Error("批量分类信息添加成功...",$mainlink."?file=category&act=list&sysgroupid=".$sysgroupid);
}
elseif($sysAct == "list")
{
	if(!$sysgroupid)
	{
		Error("操作不正确！",$mainlink."?file=sysgroup&act=list");
	}
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	$rs = $DB->qgGetOne("SELECT groupname FROM ".$prefix."sysgroup WHERE id='".$sysgroupid."'");
	$groupname = $rs["groupname"];
	unset($rs);
	#[显示列表信息]
	$rslist = $DB->qgGetAll("SELECT * FROM ".$prefix."category WHERE sysgroupid='".$sysgroupid."' AND language='".$language."' order by taxis asc");
	foreach($rslist AS $key=>$value)
	{
		if($value["fieldgroupid"])
		{
			$field = $DB->qgGetOne("SELECT * FROM ".$prefix."sysgroup_fieldgroup WHERE id='".$value["fieldgroupid"]."'");
			$value["groupname"] = $field["groupname"];
		}
		if(!$value["catename"] && $value["catename_en"])
		{
			$value["catename"] = $value["catename_en"];
		}
		if(!$value["catename"] && $value["catename_ft"])
		{
			$value["catename"] = $value["catename_ft"];
		}
		if($value["catename"] == $value["catename_en"])
		{
			unset($value["catename_en"]);
		}
		if($value["catename"] == $value["catename_ft"])
		{
			unset($value["catename_ft"]);
		}
		$rslist[$key] = $value;
	}
	$catelist = $CT->arraySet($rslist,0,"└─","6");
	Foot("category.list.qg");
}
elseif($sysAct == "delete")
{
	if(!$sysgroupid)
	{
		Error("操作不正确！",$mainlink."?file=sysgroup&act=list");
	}
	if($_SESSION["admin"]["typer"] != "system" && !$QG_AP["msg_".$sysgroupid])
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
	$id = intval($id);
	if(!$id)
	{
		Error("操作非法",$mainlink."?file=sysgroup&act=list");
	}
	#[检测是否存在子分类信息]
	$rs = $DB->qgGetOne("SELECT id FROM ".$prefix."category WHERE rootid='".$id."' OR parentid='".$id."'");
	if($rs)
	{
		Error("已存在子分类，不允许删除",$mainlink."?file=category&act=list&sysgroupid=".$sysgroupid);
	}
	#[检测是否有已审核的信息]
	$rs = $DB->qgGetOne("SElECT id FROM ".$prefix."msg WHERE cateid='".$id."'");
	if($rs)
	{
		Error("已存在内容信息，不允许删除",$mainlink."?file=category&act=list&sysgroupid=".$sysgroupid);
	}
	#[删除分类信息]
	$DB->qgQuery("DELETE FROM ".$prefix."category WHERE id='".$id."'");
	Error("分类已删除成功",$mainlink."?file=category&act=list&sysgroupid=".$sysgroupid);
}
?>